package com.situ.blog.dao.impl;


import com.situ.blog.dao.IBlogTypeDao;
import com.situ.blog.pojo.entity.BlogType;
import com.situ.blog.pojo.query.BlogTypeQuery;
import com.situ.blog.pojo.vo.BlogTypeCountVO;
import com.situ.blog.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BlogTypeDaoImpl implements IBlogTypeDao {
    @Override
    public List<BlogType> selectByPage(BlogTypeQuery blogTypeQuery) {
        int offset = (blogTypeQuery.getPage() - 1) * blogTypeQuery.getLimit();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<BlogType> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,deleted,gmt_create,gmt_modified FROM blog_type where 1=1 ";
            //这里面放所有搜索条件
            List<Object> queryList = new ArrayList<>();
            String queryName = blogTypeQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add("%" + queryName + "%");
            }
            sql += " limit ?,?";
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            preparedStatement.setInt(queryList.size() + 1, offset);
            preparedStatement.setInt(queryList.size() + 2, blogTypeQuery.getLimit());

            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int deleted = resultSet.getInt("deleted");
                java.util.Date gmtCreate = resultSet.getTimestamp("gmt_create");
                java.util.Date gmtModified = resultSet.getTimestamp("gmt_modified");
                BlogType blogType = new BlogType(id, name, deleted, gmtCreate, gmtModified);
                list.add(blogType);
            }
            for (BlogType blogType : list) {
                System.out.println(blogType);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public Integer selectTotalCount(BlogTypeQuery blogTypeQuery) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        int totalCount = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select count(*) from blog_type where 1=1 ";
            List<Object> queryList = new ArrayList<>();
            String queryName = blogTypeQuery.getName();
            if (queryName != null && !"".equals(queryName)) {
                sql += " and name like ? ";
                queryList.add(queryName);
            }
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < queryList.size(); i++) {
                preparedStatement.setObject(i + 1, queryList.get(i));
            }
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                totalCount = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return totalCount;
    }

    @Override
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from blog_type where id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public int add(BlogType blogType) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into blog_type(name) values(?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, blogType.getName());
            System.out.println(preparedStatement);
            count = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public List<BlogTypeCountVO> selectBlogTypeCount() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<BlogTypeCountVO> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT bt.name,count(*) AS `value`\n" +
                    "FROM blog AS b INNER JOIN blog_type AS bt\n" +
                    "ON b.type_id=bt.id\n" +
                    "GROUP BY b.type_id";
            preparedStatement = connection.prepareStatement(sql);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                String name = resultSet.getString("name");
                int value = resultSet.getInt("value");
                BlogTypeCountVO blog = new BlogTypeCountVO(name, value);
                list.add(blog);
            }
            for (BlogTypeCountVO blogTypeCountVO : list) {
                System.out.println(blogTypeCountVO);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }

    @Override
    public List<BlogType> selectAll() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<BlogType> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "SELECT id,name,deleted,gmt_create,gmt_modified FROM blog_type";
            preparedStatement = connection.prepareStatement(sql);
            System.out.println(preparedStatement);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {//判断下一个有没有，如果返回true而且指向下一个，没有返回false
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int deleted = resultSet.getInt("deleted");
                java.util.Date gmtCreate = resultSet.getTimestamp("gmt_create");
                java.util.Date gmtModified = resultSet.getTimestamp("gmt_modified");
                BlogType blogType = new BlogType(id, name, deleted, gmtCreate, gmtModified);
                list.add(blogType);
            }
            for (BlogType blogType : list) {
                System.out.println(blogType);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, preparedStatement, resultSet);
        }

        return list;
    }


}
